package com.wwwc.admin.web.servlet;

import java.util.*;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;

import com.wwwc.util.web.*;

public class adminAccessLogServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,  HttpServletResponse response)
	throws IOException, ServletException
    {
	doPost(request,response);
    }

    public void doPost(HttpServletRequest request,  HttpServletResponse response)
	throws IOException, ServletException
    {
        response.setContentType("text/html");
	
	HttpSession session = request.getSession(true);
        PrintWriter out     = response.getWriter();
	String call_path = request.getContextPath()+request.getServletPath();

	String action    = request.getParameter("action");
	
	if(action == null) {
	    out.println("<HTML><HEAD><TITLE></TITLE>");
	    out.println("</HEAD><BODY>");
	    out.println("<FORM action="+call_path+">");
	    out.println("<TABLE border=1>");
	    out.println("<TR><TD align=center>");
	    out.println("<input type=text name=file_name value='F:/ApacheGroup/Apache2/logs/access.log' size=70 style='font-size: 8pt'>");
	    out.println("</TD><TD>");
	    out.println("<input type=submit name=B1 value=View style='font-size: 8pt'>");
	    out.println("</TD></TR>");
	    out.println("<input type=hidden name=action value=load_file>");
	    out.println("</TABLE>");
	    out.println("</FORM>");
	    out.println("</BODY></HTML>");
	    return;
	}

	if(action.equals("load_file")) {
	    String file_name = request.getParameter("file_name");
	    if(file_name==null) {
		out.println("Please select your log file!");
		return;
	    }

	    System.out.println("file_name="+file_name);

	    File f = new File(file_name);
	    
	    out.println("<HTML><HEAD><TITLE></TITLE>");
	    out.println("</HEAD><BODY>");

	    String filter = "";
	    BufferedReader brd =  new BufferedReader(new FileReader(file_name));
	    String temp = null;
	    out.println("<TABLE whidth=100% style='font-size: 8pt'><TR><TD>");
	    while ((temp = brd.readLine()) != null) {
		if(temp != null && !temp.startsWith("192.168.1.10") && temp.length()>1) {
		    if(filter.equals("")) { 
			out.println(temp+"<BR>");
		    } 
		    else {
			if(temp.indexOf(filter) != -1) {
			    out.println(temp+"<BR>");
			} 
		    } 
		} 
	    } 
	    brd.close();
	    out.println("</TD></TR></TABLE>");
	    out.println("</BODY></HTML>");
	}
	else {
	    out.println("action="+action);
	}
    }
}
